package tree;

/**
 * 二叉树
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {

        //测试
        //创建一个二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的节点
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "林冲5");
        HeroNode node6 = new HeroNode(6, "林冲6");
        HeroNode node7 = new HeroNode(7, "林冲7");

        binaryTree.setRoot(root);

        //手动创建二叉树
        root.setLeft(node2);
        node2.setLeft(node4);
        node2.setRight(node5);

        root.setRight(node3);
        node3.setLeft(node6);
        node3.setRight(node7);

//        binaryTree.preOrder();
//        System.out.println("--------");
//        binaryTree.inficOrder();
//        System.out.println("------");
//        binaryTree.postOrder();

//        binaryTree.preOrdersearch(5);

        binaryTree.preOrder();
        binaryTree.delNode(2);
        binaryTree.preOrder();

    }
}

//定义一个二叉树
class BinaryTree{
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    //前序遍历
    public void preOrder(){
        if(this.root !=null){
            this.root.preOrder();
        }else{
            System.out.println("当前二叉树为空");
        }
    }

    //中序遍历
    public void inficOrder(){
        if(this.root !=null){
            this.root.inficOrder();
        }else {
            System.out.println("当前二叉树为空");
        }
    }

    //后续遍历
    public void postOrder(){
        if(this.root !=null){
            this.root.postOrder();
        }else {
            System.out.println("当前二叉树为空");
        }
    }

    //前序遍历查找
    public void preOrdersearch(int no){
        if(root !=null){
            HeroNode heroNode = this.root.preOrdersearch(no);
            System.out.println("找到了节点："+heroNode);
        }else{
            System.out.println("当前二叉树为空");
        }
    }

    //中序遍历查找
    public void inficOrdersearch(int no){
        if(root !=null){
            HeroNode heroNode = this.root.inficOrdersearch(no);
            System.out.println("找到了节点："+heroNode);
        }else{
            System.out.println("当前二叉树为空");
        }
    }

    //后续遍历查找
    public void postOrdersearch(int no){
        if(root !=null){
            HeroNode heroNode = this.root.postOrdersearch1(no);
            System.out.println("找到了节点："+heroNode);
        }else{
            System.out.println("当前二叉树为空");
        }
    }

    //删除节点
    public void delNode(int no){
        if(root !=null){
            if(root.getNo() ==  no){
                root = null;
            }else{
                root.delNode(no);
            }
        }else{
            System.out.println("节点为空，不能删除");
        }
    }

}

//创建HeroNode节点
class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
            this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //删除节点
    public void delNode(int no){
        if(this.left !=null && this.left.no == no){
            this.left = null;
            return;
        }
        if(this.right !=null && this.right.no == no){
            this.right = null;
            return;
        }
        if(this.left != null){
            this.left.delNode(no);
        }
        if(this.right != null){
            this.right.delNode(no);
        }
    }

    //节点的方法
    //前序遍历的方法
    public void preOrder(){
        System.out.println(this);  //先输出父节点
        //递归左子树
        if(this.left !=null){
            this.left.preOrder();
        }
        //递归向右子树
        if(this.right !=null){
            this.right.preOrder();
        }
    }

    //中序遍历
    public void inficOrder(){
        //递归向左子树递归
        if(this.left !=null){
            this.left.inficOrder();
        }
        //输出当前节点
        System.out.println(this);

        //递归向右子树递归
        if(this.right !=null){
            this.right.inficOrder();
        }
    }

    //后序遍历
    public void postOrder(){
        if(this.left !=null){
            this.left.postOrder();
        }
        if(this.right !=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

    //前序遍历查找

    /**
     *
     * @param no 待查找的编号
     * @return
     */
    public HeroNode preOrdersearch(int no){
        System.out.println("-------");
        //比较当前节点是不是
        if(this.no == no){
            return this;
        }
        //左递归查找
        HeroNode resNode = null;
        if(this.left != null){
            resNode = this.left.preOrdersearch(no);
        }

        if(resNode != null){
            //说明左递归找到了
            return resNode;
        }

        //向右递归
        if(this.right != null){
            resNode = this.right.preOrdersearch(no);
        }

        return resNode;  //这个找没找到都的返回
    }

    //中序遍历查找
    public HeroNode inficOrdersearch(int no){

        //左递归查找
        HeroNode resNode = null;
        if(this.left != null){
            resNode = this.left.inficOrdersearch(no);
        }
        if(resNode != null){
            return resNode;
        }
        System.out.println("-----");
        if(this.no == no){
            return this;
        }

        //向右递归
        if(this.right != null){
            resNode = this.right.inficOrdersearch(no);
        }

        return resNode;
    }

    //后序遍历查找
    public HeroNode postOrdersearch1(int no){
        HeroNode resNode = null;


        //向左递归
        if(this.left != null){
            resNode = this.left.postOrdersearch1(no);
        }
        if(resNode != null){
            return resNode;
        }
        //向右递归
        if(this.right != null){
            resNode = this.right.postOrdersearch1(no);
        }
        if(resNode != null){
            return resNode;
        }

        System.out.println("-----");
        if(this.no == no){
            return this;
        }
        return resNode;

    }
}


